package com.drinks.yiniang.ui.activity.camera;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.photo.DraftsPhotosAdapter;
import com.drinks.yiniang.common.recycler.decoration.DraftsManageItemDecoration;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.FileUtils;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.listener.OnSureCancelListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.dialog.MaterialDialog;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 草稿管理
 *
 * @author han
 * @date 2019/09/16
 */
public class DraftsManageActivity extends BaseTranBarActivity implements OnNavgationBarClickListener, OnSureCancelListener {

    private NavigationBarLayout nav_bar_drafts_manage;
    private BaseRecyclerView rcv_drafts_manage;

    private DraftsPhotosAdapter mDraftsPhotosAdapter;

    private boolean isNeedSetResult;

    @Override
    protected void initView() {
        nav_bar_drafts_manage = findViewById(R.id.nav_bar_drafts_manage);

        rcv_drafts_manage = findViewById(R.id.rcv_drafts_manage);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();
    }

    private void initAdapter() {
        rcv_drafts_manage.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 3));
        rcv_drafts_manage.addItemDecoration(DraftsManageItemDecoration.getItemDPDecoration(10));

        mDraftsPhotosAdapter = new DraftsPhotosAdapter(mContext, new ArrayList<Photos>());
        mDraftsPhotosAdapter.setHeaderAndEmpty(true);
        mDraftsPhotosAdapter.setCommonEmpty(getLayoutInflater(), rcv_drafts_manage);
        mDraftsPhotosAdapter.bindToRecyclerView(rcv_drafts_manage);

        List<String> draftFiles = FileUtils.getAllDraftFiles(Constants.RootDrafts);
        List<Photos> draftsList = new ArrayList<>();

        for(String path: draftFiles) {
            Photos photos = new Photos();

            photos.path = path;

            draftsList.add(photos);
        }

        mDraftsPhotosAdapter.addData(draftsList);
    }

    @Override
    protected void initListener() {
        nav_bar_drafts_manage.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_drafts_manage;
    }

    @Override
    public void onBackClick(View v) {
        doFinish();
    }

    @Override
    public void onEditClick(View v) {
        boolean isChooseOne = false;

        for(Photos item: mDraftsPhotosAdapter.getData()) {
            if(item.isChecked) {
                isChooseOne = true;
                break;
            }
        }

        if(!isChooseOne) {
            return;
        }

        //删除草稿
        MaterialDialog dialog = MaterialDialog.newInstance(getString(R.string.text_delete_draft_sure));
        dialog.show(getSupportFragmentManager(), Constants.TAG_TIPS_DIALOG);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            doFinish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void doFinish() {
        if(isNeedSetResult) {
            isNeedSetResult = false;
            setResult(Constants.RESULT_CODE_NORMAL);
        }

        finish();
    }

    @Override
    public void onSure(int type, String value) {
        isNeedSetResult = true;

        for(int i = mDraftsPhotosAdapter.getData().size() - 1; i >= 0; i--) {
            if(mDraftsPhotosAdapter.getItem(i).isChecked) {
                FileUtils.deleteFile(mDraftsPhotosAdapter.getItem(i).path);
                mDraftsPhotosAdapter.remove(i);
            }
        }
    }

    @Override
    public void onCancel(int type) {

    }
}
